package me.cell.net
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	public class SocketBridge extends Socket
	{
		private static const P_HEAD_LENGTH:int = 2;
		private var _host:String;
		private var _port:int;
		
		//当发生断包时，用来做为缓存
		private var _temp:ByteArray;
		private var _headLen:int = 0;
		public function SocketBridge()
		{
			_temp = new ByteArray();
			
			addEventListener(ProgressEvent.SOCKET_DATA, onData);
			addEventListener(Event.CONNECT, onConnect);
			addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		/**
		 * 连接成功
		 * @param evt 
		 */		
		private function onConnect(evt:Event):void
		{
			trace("connected ...");
//			AppFacade.getInstance().sendNotification(SocketNotification.CONNECT_RESULT,
//				{
//					result:0
//				});
//			var ba:ByteArray = new ByteArray();
//			ba.writeUTFBytes("sdji");
//			send(ba);
		}
		
		/**
		 * io错误
		 * @param evt 
		 */		
		private function onIOError(evt:IOErrorEvent):void
		{
			trace(evt.text);
		}
		
		/**
		 * 安全策略错误
		 * @param evt 
		 */		
		private function onSecurityError(evt:SecurityErrorEvent):void
		{
			trace(evt.text);
		}
		
		/**
		 * 接受到数据后，进行粘包和碎包的处理
		 * @param evt 
		 */		
		private function onData(evt:ProgressEvent):void
		{
			var socketRespArr:ByteArray = new ByteArray();
			socketRespArr.writeBytes(_temp);//把缓存的内容先放入空间里
			_temp.clear();
			
			var preArr:ByteArray = new ByteArray();
			this.readBytes(preArr);//只是用来存储收到的二进制流
			socketRespArr.writeBytes(preArr);
			socketRespArr.position = 0;
			
			while(socketRespArr.bytesAvailable >= P_HEAD_LENGTH)
			{//如果至少有一个完整的包头长度，开始截包循环
				var headResp:ByteArray = new ByteArray();
				var bodyLen:int = 0;
				socketRespArr.readBytes(headResp, 0, P_HEAD_LENGTH);
				bodyLen = headResp.readShort();
				if(socketRespArr.bytesAvailable >= bodyLen)
				{//如果含有完整的该包本体，或者超出本体长度
					
					var packet:ByteArray = new ByteArray();
					socketRespArr.readBytes(packet, 0, bodyLen);
//					this.dispatchEvent(new PacketRcvEvent(packet));
				}
				else
				{//如果该包的本体长度不到，把原来的包头和剩余数据放入缓存
					
					_temp.writeShort(bodyLen);//包头写入
					_temp.writeBytes(socketRespArr, socketRespArr.position);
					break;
				}
			}
			
			//这是对如果有包头断裂，导致没有进入循环的残余数据流放入缓存区
			_temp.writeBytes(socketRespArr, socketRespArr.position);
			_temp.position = 0;
		}
		
		public function send(ba:ByteArray):void
		{
			if(connected)
			{
				for(var i:int = 0; i < 50; i++)
				{
					this.writeBytes(ba);
					this.writeUTFBytes(i.toString());
					this.flush();
				}
			}
		}
		
		public function doConnect():void
		{
			super.connect(_host, _port);
		}
		
		override public function connect(host:String, port:int):void
		{
			_host = host;
			_port = port;
			super.connect(_host, _port);
		}

		public function set host(value:String):void
		{
			_host = value;
		}

		public function set port(value:int):void
		{
			_port = value;
		}

		//
	}
}